
/*
 *
 * 名称：周期
 * 功能：循环和不循环功能的基本父类封装
 * 作者：清河张
 * 时间：2025.05.13
 *
 */


#ifndef ZHOUQI_H
#define ZHOUQI_H
#include "xzqm.h"

enum {INVALIDID = -1};


class CT
{
public:
    //默认构造函数
    CT();
    CT(int id, const char* name);
    int id() const;
    const char* name() const;
    bool null() const;
    bool operator== (int id) const;
    bool operator== (const CT& ct) const;
private:
    int _id;
    const char* _name;
};



template <class T, int N>
class DIEDAI //迭代
{
public:
    DIEDAI(T* b) : _begin(b), _end(b+N)
    {

    }
    T* begin()
    {
        return _begin;
    }
    T* end()
    {
        return _end;
    }
private:
    T* _begin;
    T* _end;
};





// 周期类
template <class T, int N>
class ZHOUQI : public T
{
public:
    typedef ZHOUQI<T,N>  THISCLASS;
    typedef DIEDAI<T,N> DIEDAI_T;

    ZHOUQI(int n):T(invalid(n)?T():_objs[n])
    {

    }

    ZHOUQI(const T& t):T(t)
    {
    }

    ZHOUQI():T()
    {
    }

    auto& operator = (const T& t)
    {
        return T::operator = (t);
    }

    auto& operator = (int n)
    {
        if(invalid(n))
            *this = T();
        else
            *this = _objs[n];
        return *this;
    }

    bool operator == (const T& t) const
    {
        return !invalid()&&T::operator == (t);
    }

    bool operator == (int n) const
    {
        return !invalid()&&T::operator == (n);
    }

    int operator + (const T& t) const
    {
        if(invalid() || t.null()) return -1;
        return add(t.id());
    }

    int operator - (const T& t) const
    {
        if(invalid() || t.null()) return -1;
        return add(-t.id());
    }

    THISCLASS operator + (int n) const
    {
        return add(n);
    }

    THISCLASS operator - (int n) const
    {
        return add(-n);
    }

    THISCLASS& operator += (int n) const
    {
        *this = add(n);
        return *this;
    }

    THISCLASS& operator -= (int n) const
    {
        *this = add(-n);
        return *this;
    }

    bool invalid() const
    {
        return invalid(T::id());
    }

    bool invalid(int n) const
    {
        return n >= N || n < 0;
    }

    template <class CONTAINER>
    static void push_back_name(CONTAINER& container)
    {
        for( int i = 0; i < N; ++i )
        {
            container.push_back(_objs[i].name());
        }
    }

    template <class CONTAINER>
    static void append_name(CONTAINER& container)
    {
        for( int i = 0; i < N; ++i )
        {
            container.append(_objs[i].name());
        }
    }

    static int size()
    {
        return N;
    }

    static DIEDAI_T diedai()
    {
        return DIEDAI_T(_objs);
    }

protected:
    int add(int n) const
    {
        if(invalid())
        {
            return T::id();
        }
        return adjust(T::id() + n);
    }

    int adjust(int n) const
    {
        return XUNHUAN ( n, N );
    }

    static const T &index(int n)
    {
        return _objs[n%N];
    }


private:

    static T _objs[N];
};


//列表类
template <class T, int N>
class LIEBIAO : public T
{
public:
    typedef LIEBIAO<T,N>  THISCLASS;
    typedef DIEDAI<T,N> DIEDAI_T;

    LIEBIAO(int n):T( invalid(n) ? T() :_objs[n] )
    {
    }

    LIEBIAO():T()
    {
    }

    LIEBIAO(const T& t):T( t )
    {
    }

    auto& operator = (const T& t)
    {
        return T::operator = (t);
    }

    auto& operator = (int n)
    {
        if(invalid(n))
            *this = T();
        else
            *this = _objs[n];
        return *this;
    }

    bool operator == (const T& t) const
    {
        return T::operator == (t);
    }

    bool operator == (int n) const
    {
        return T::operator == (n);
    }

    bool invalid() const
    {
        return invalid(T::id());
    }

    bool invalid(int n) const
    {
        return n >= N || n < 0;
    }

    static int size()
    {
        return N;
    }

    template <class CONTAINER>
    static void push_back_name(CONTAINER& container)
    {
        for( int i = 0; i < N; ++i )
        {
            container.push_back(_objs[i].name());
        }
    }

    template <class CONTAINER>
    static void append_name(CONTAINER& container)
    {
        for( int i = 0; i < N; ++i )
        {
            container.append(_objs[i].name());
        }
    }

    static DIEDAI_T diedai()
    {
        return DIEDAI_T(_objs);
    }

protected:

    static const T &index(int n)
    {
        return _objs[n%N];
    }


private:
    static T _objs[N];
};


#endif // ZHOUQI_H
